
import abspkg from '../../graph/action/absaction.js'
import {LGXEnums} from '../../common/sysenum.js'

import utilfuncs from '../../common/tools/utilfuncs.js';
var funcs = utilfuncs.UtilFuncs;

class EllipseGraphAction extends abspkg.AbsGraphAction {
    constructor(graph) {
        super(graph);
    }

    act2Mouse(mosEvnt, worldPos) {
        var graph = this.getRefGraph();
        if (graph == null) {
            return LGXEnums.LGXPlotState.ErrorState;
        }
        var state = LGXEnums.LGXPlotState.NoneState;
        var postion = graph.getPos();
        if (mosEvnt.type == LGXEnums.LGXMouseMode.LMouseDown) {
            if (graph.getStart() == 0) {
                graph.setStart(1);
                postion.x = worldPos.x;
                postion.y = worldPos.y;
                state = LGXEnums.LGXPlotState.AppendPtState;
                graph.mouseDownTime = 1;
            }
            else if (graph.mouseDownTime == 1) {
                var detax = worldPos.x - postion.x;
                var detay = worldPos.y - postion.y;
                var a = Math.pow(detax, 2);
                var b = Math.pow(detay, 2);
                var r = Math.sqrt(a + b);
                postion.yRadius = r;
                postion.xRadius = r;
                state = LGXEnums.LGXPlotState.AppendPtState;
                graph.mouseDownTime = 2;
            }
            else if (graph.mouseDownTime == 2) {
                var detax = worldPos.x - postion.x;
                var detay = worldPos.y - postion.y;
                var a = Math.pow(detax, 2);
                var b = Math.pow(detay, 2);
                var r = Math.sqrt(a + b);
                postion.yRadius = r;
                graph.setFinish(1);
                state = LGXEnums.LGXPlotState.FinistState;
                graph.mouseDownTime = 0;
            }
        }
        else if (mosEvnt.type == LGXEnums.LGXMouseMode.mouseMove
            || mosEvnt.type == LGXEnums.LGXMouseMode.mouseDrag) {
            detax = worldPos.x - postion.x;
            detay = worldPos.y - postion.y;
            a = Math.pow(detax, 2);
            b = Math.pow(detay, 2);
            let r = Math.sqrt(a + b);

            if (graph.mouseDownTime == 1) {
                postion.yRadius = r;
                postion.xRadius = r;
                state = LGXEnums.LGXPlotState.OKState;
            }
            else {
                postion.yRadius = r;
                state = LGXEnums.LGXPlotState.OKState;
            }

        }
        graph.calScope(1);
        return state;
    }

    modifyByCMD(isNew, cmd) {
        var graph = this.getRefGraph();
        if (graph.getStart() == 1 && graph.getFinish() == 1) {
            return null;
        }

        let rst = null;
        var position = graph.getPos();
        let coordLst = cmd.split(",");
        cmd = cmd.toLowerCase();

        if (cmd == "e") {
            let finishFlag = false;
            let nextTips = "";
            if (graph.mouseDownTime == 1) {
                graph.mouseDownTime = 2;
                nextTips = "输入单个数字确定短轴半径值(yR)或输入(x,y)或在图中点击确定半径。x,y,相对坐标使用:@x,y。使用当前位置（e）";
            }
            else if (graph.mouseDownTime == 2) {
                finishFlag = true;
            }

            let cmd = { type: 'e', value: "" };
            let appendToPreCMD = "";
            rst = { cmdObj: cmd, nextTips, appendToPreCMD, finish: finishFlag };

            if (finishFlag){
                graph.setFinish(1);
                graph.mouseDownTime = 0;
            }
        }
        else if (coordLst.length == 1) {
            let rdesc = coordLst[0];
            if (isNew) {
                return null;
            }
            else if (graph.getStart() == 1) {
                let finishFlag = false;
                let nextTips = "";
                if (graph.mouseDownTime == 1) {
                    position.yRadius = parseFloat(rdesc);
                    position.xRadius = position.yRadius;
                    graph.mouseDownTime = 2;
                    nextTips = "输入单个数字确定短轴半径值(yR)或输入(x,y)或在图中点击确定半径。x,y,相对坐标使用:@x,y。使用当前位置（e）";
                }
                else if (graph.mouseDownTime == 2) {
                    position.yRadius = parseFloat(rdesc);
                    finishFlag = true;
                }

                let cmd = { type: 'draw_lmousedown', value: rdesc };
                let appendToPreCMD = "";
                rst = { cmdObj: cmd, nextTips, appendToPreCMD, finish: finishFlag };
                if (finishFlag){
                    graph.setFinish(1);
                    graph.mouseDownTime = 0;
                }
            }
        }
        else if (coordLst.length == 2) {
            let xdesc = coordLst[0];
            let ydesc = coordLst[1];

            if (isNew) {
                if (xdesc.indexOf("@") >= 0) {
                    return null;
                }
                let x = parseFloat(xdesc);
                let y = parseFloat(ydesc);
                position.x = x;
                position.y = y;
                position.xRadius = 1;
                position.yRadius = 1;
                position.stAngle = 0;
                position.endAngle = 360;

                graph.mouseDownTime = 1;
                graph.setStart(1);

                let coordDecs = x.toFixed(2) + "," + y.toFixed(2);
                let cmd = { type: 'draw_lmousedown', value: coordDecs };
                let nextTips = "输入单个数字确定长轴半径值(xR)或输入(x,y)或在图中点击确定半径。x,y,相对坐标使用:@x,y。使用当前位置（e）";
                let appendToPreCMD = coordDecs;
                rst = { cmdObj: cmd, nextTips, appendToPreCMD, finish: false };
            }
            else if (graph.getStart() == 1) {
                let cnt = { x: position.x, y: position.y };

                let nextTips = "";
                let x = parseFloat(xdesc);
                let y = parseFloat(ydesc);
                let XRelateIdx = xdesc.indexOf("@");
                if (XRelateIdx >= 0) {
                    let tmp = xdesc.substr(XRelateIdx + 1, xdesc.length - XRelateIdx - 1);
                    x = cnt.x + parseFloat(tmp);
                    y = cnt.y + y;
                }
                let finishFlag = false;
                let curentPt = { x, y };
                if (graph.mouseDownTime == 1) {
                    position.yRadius = funcs.distancePoint(curentPt, cnt, 1);
                    position.xRadius = position.yRadius;
                    nextTips = "输入单个数字确定短轴半径值(yR)或输入(x,y)或在图中点击确定半径。x,y,相对坐标使用:@x,y。使用当前位置（e）";
                }
                else if (graph.mouseDownTime == 2) {
                    finishFlag = true;
                    position.yRadius = funcs.distancePoint(curentPt, cnt, 1);
                }

                let coordDecs = x.toFixed(2) + "," + y.toFixed(2);
                let cmd = { type: 'draw_lmousedown', value: coordDecs };
                
                let appendToPreCMD = "";
                rst = { cmdObj: cmd, nextTips, appendToPreCMD, finish: finishFlag };
                if (finishFlag){
                    graph.setFinish(1);
                    graph.mouseDownTime = 0;
                }
            }
        }
        graph.calScope(1);
        return rst;
    }
}

export default { EllipseGraphAction }
